package com.hyfrogx.modules.manage.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyfrogx.common.qcloud.UploadUtils;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.entity.CoindogLiveEntity;
import com.hyfrogx.modules.manage.form.flash.FlashForm;
import com.hyfrogx.modules.manage.form.flash.FlashVo;
import com.hyfrogx.modules.manage.service.HyFlashLiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/9/26 14:27
 * @Company 汉咏科技
 * 快讯控制类
 */
@RestController
@RequestMapping("/flash")
@Api(value = "HyFlashController", tags = {"后台--快讯管理"})
public class HyFlashController {

    @Autowired
    private HyFlashLiveService hyFlashLiveService;
    /**
     * 金色财经拉取最新快讯信息，并保存到数据库
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("coindog/get")
    public R getCoindog()
    {
        return hyFlashLiveService.getCoindog();

    }


    /**
     * 从数据库获取快讯信息
     * @param map
     * @return
     */
    @PostMapping("page/getFromSql")
    @ApiOperation("从数据库获取快讯信息")
    public R getByPageFromSql(@RequestBody Map map)
    {
        QueryExt<CoindogLiveEntity> queryExt = new QueryExt<>(map);
        Map pmap = new HashMap();
        pmap.putAll(queryExt.getCondition());
        IPage<FlashVo> iPage = hyFlashLiveService.getByPageFromSql(queryExt,pmap);
        return R.ok().put("page",iPage);
    }

    /**
     * 编辑单条快讯信息
     * @return
     */
    @PostMapping("/live/updateOne")
    @ApiOperation("编辑单条快讯信息")
    public R updateCoindogLive(@RequestBody FlashForm flashForm)
    {
        CoindogLiveEntity coindogLiveEntity = BeanUtil.toBean(flashForm,CoindogLiveEntity.class);
        if (!hyFlashLiveService.updateLiveOne(coindogLiveEntity))
        {
            return R.error("修改失败");
        }

        return R.ok();
    }
    @PostMapping("/uploadimg")
    @ApiOperation("快讯图片上传")
    public R upload(@RequestParam("file") MultipartFile file) {
        String result = null;
        if(file!=null){
            String fileName= UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
            result= UploadUtils.upload(UploadUtils.convertFile(file),fileName, FrogConstant.Path.FLASH);
        }
        return R.ok().put("img_url",result);
    }

    /**
     * 批量保存快讯信息
     */
    @PostMapping("/live/updateList")
    public R updateCoindogLiveList(@RequestBody List<CoindogLiveEntity> coindogLiveEntities)
    {
        if(!hyFlashLiveService.updateLiveList(coindogLiveEntities))
        {
            return R.error("批量修改失败");
        }

        return R.ok();
    }

    /**
     * 单个快讯上下架
     */
    @PostMapping("/live/updown/{id}")
    @ApiOperation("单个快讯上下架")
    public R updownCoindogLiveOne(@PathVariable Long id)
    {
        if (!hyFlashLiveService.updateStatusOne(id))
        {
            return R.error("状态修改失败");
        }
        return R.ok();
    }

    /**
     * 批量上架快讯
     * @param id
     * @return
     */
    @PostMapping("/live/upList")
    @ApiOperation("批量上架快讯")
    public R LiveUpList(Long[] id)
    {
        hyFlashLiveService.LiveUpList(id);
        return R.ok();
    }

    /**
     * 批量下架快讯
     * @param id
     * @return
     */
    @PostMapping("/live/downList")
    @ApiOperation("批量下架快讯")
    public R LiveDownList(Long[] id)
    {
        hyFlashLiveService.LiveDownList(id);
        return R.ok();
    }

    /**
     * 根据id删除单个快讯（物理删除）
     * @param id
     * @return
     */
    @PostMapping("/live/deleteOne/{id}")
    @ApiOperation("根据id删除单个快讯")
    public R liveDeleteOne(@PathVariable(value = "id") Long id)
    {
        if (!hyFlashLiveService.deleteLiveOne(id))
        {
            return R.error("快讯删除失败");
        }
        return R.ok();
    }

    /**
     * 批量删除快讯（物理删除）
     * @param id
     * @return
     */
    @PostMapping("/live/deleteList")
    @ApiOperation("批量删除快讯")
    public R liveListUp(Long[] id)
    {
        hyFlashLiveService.deleteLiveList(id);
        return R.ok();
    }
}
